home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / VISUALBA / VBPRINT.ZIP / VBPRINT.C < prev    next >
C/C++ Source or Header  |  1993-03-09  |  48KB  |  1,409 lines

  1. /*************************************************************************
  2.     Print Engine for Visual Basic
  3.     Printing utilities for hardcopy output
  4.  
  5.     Written by Barry Nolte
  6.  
  7.     06-28-92    File Created
  8.     11-12-92    Added Graphics Functions
  9.     01-17-93    General Cleanup of Sources
  10.     02-03-93    Simple Bug Fixes
  11.  
  12. *************************************************************************/
  13.  
  14. // COPYRIGHT:
  15. //
  16. //   (C) Copyright Microsoft Corp. 1993.  All rights reserved.
  17. //
  18. //   You have a royalty-free right to use, modify, reproduce and
  19. //   distribute the Sample Files (and/or any modified version) in
  20. //   any way you find useful, provided that you agree that
  21. //   Microsoft has no warranty obligations or liability for any
  22. //   Sample Application Files which are modified.
  23. //
  24.  
  25. #include <windows.h>
  26. #include <drivinit.h>
  27. #include <commdlg.h>
  28. #include <memory.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "vbprint.h"
  32.  
  33. // DLL Version Numbers
  34. #define MAJOR_VERSION     1
  35. #define MINOR_VERSION     0
  36.  
  37. #define BORDER_WIDTH    2        // Default border in points
  38.  
  39. /*************************************************************************
  40.  
  41.     GLOBAL VARIABLES
  42.  
  43. *************************************************************************/
  44.  
  45. static    HANDLE        hInst;        // Handle to calling app
  46. static    HWND        hWndApp;    // Handle ot calling apps main window
  47. static    PRINTDLG    ppPrinter;    // Structure of printer properties
  48.  
  49. // Misc Procs
  50. static    FARPROC        lpfnPrintDlgProc;    // Printing dialog proc
  51. static    FARPROC        lpfnAbortProc;        // Printing about proc
  52. static  BOOL        fAbort;
  53.  
  54. static    struct tagPrinterInfo    // Information on printer needed to place objects correctly
  55.     {
  56.         POINT    ptPageSize;            // Size of the page
  57.         RECT    rcUnprintable;        // Unprintable regions of the printer
  58.         RECT    rcMargins;            // Margins that are set in the Page Layout Function
  59.         POINT    ptResolution;        // Printer Resolution in device DPI
  60.  
  61.     } PrinterInfo;
  62.  
  63. static    struct tagPageInfo        // Information on where I'm printing on the page
  64.     {
  65.         POINT    ptCurrentPos;        // Current Print Position
  66.         DWORD    dwCurLineWidth;        // Current Line Width, good for multiple calls to ParagraphText
  67.         int        nTabStop;            // Current Tab Stop value
  68.         BOOL    bPageDirty;            // Is the page dirty?
  69.  
  70.     } PageInfo;
  71.  
  72. static struct tagParagraphInfo    // Information on paragraph attributes
  73.     {
  74.         HFONT        hFont;            // Handle to paragraphs default font
  75.         HFONT        hOldFont;        // Handle to previous font
  76.         TEXTMETRIC    tm;                // Text metric struct that has info on the font
  77.         DWORD        dwWidth;        // Width of column to print into
  78.         int            nStyle;            // Border and Font Attributes
  79.         WORD        wBorderWidth;    // Border Width in device units (HIBYTE = Horizontal, LOBYTE = Vertical)
  80.         int            nWidth[LAST_CHAR - FIRST_CHAR + 1];    // Widths of all the fonts characters
  81.         int            nSpaceBefore;        // Space before Paragraph in Points
  82.         int         nSpaceAfter;        // Space After Paragraph in Points
  83.         int            nLeftIndent;        // Paragraph Left Indent size, good for check box justification
  84.  
  85.     } ParagraphInfo;
  86.  
  87. static struct tagColumnInfo        // Information on printing columns
  88.     {
  89.         HFONT        hFont;                            // Handle to paragraphs default font
  90.         HFONT        hOldFont;                        // Handle to previous font
  91.         TEXTMETRIC    tm;                                // Text Metric struct that has info on the font
  92.         WORD        wBorderWidth;                    // Border Width in device units (HIBYTE = Horizontal, LOBYTE = Vertical)
  93.         int         nNumColumns;                    // Number of vertical Columns
  94.         int         nCellHeight;                    // Height of Cells
  95.         int         rgColumnWidths[MAX_COLUMNS];        // Widths of Columns
  96.         int            nWidth[LAST_CHAR - FIRST_CHAR + 1];    // Widths of all the fonts characters
  97.         int             nStyle;                            // Border and Font attributes
  98.  
  99.     } ColumnInfo;
  100.  
  101. /*************************************************************************
  102.     PROTOTYPES
  103. *************************************************************************/
  104.  
  105. int     FAR PASCAL     PrinterSetup(HWND hWnd);
  106. int     FAR PASCAL     InitializePrinter(HWND hWnd);
  107. int     FAR PASCAL    PageLayoutSetup(int nTop, int nRight, int nBottom, int nLeft);
  108. BOOL                DoCommDlgError(HWND hWnd, DWORD dError);
  109. int     FAR PASCAL     DonePrinting(void);
  110. int     FAR PASCAL     StartParagraph(LPSTR szFontName, int nFontSize, int nStyle);
  111. int     FAR PASCAL    FinishParagraph(void);
  112. int     FAR PASCAL     PrintHeadline(LPSTR szHeadLine, LPSTR szFontName, int nFontSize, int nStyle);
  113. int     FAR PASCAL    ParagraphText(LPSTR szText);
  114. int     FAR PASCAL    EjectPage (void);
  115. int                 TextOutAtCurPos(LPSTR szText);
  116. int                 GetColumnOffset(int nColumn);
  117. int                 CellDrawText(HDC hDC, LPSTR szText, LPRECT rc);
  118. BOOL                GetUnprintableRegion(HDC hDCPrn);
  119. BOOL                GetPrinterResolution(HDC hDCPrn);
  120. BOOL    FAR PASCAL    PrnAbortProc(HDC hDCPrn, short nCode);
  121. BOOL    FAR PASCAL    PrintDlgProc(HWND hDlg, WORD message, WORD wParam, LONG lParam);
  122. int     FAR PASCAL    PrintDLLVersion(VOID);
  123. BOOL    FAR PASCAL    SetParagraphSpacing(int nSpaceBefore, int nSpaceAfter);
  124. BOOL    FAR PASCAL    SetBorderWidth(int nWidth);
  125. int     FAR PASCAL    SetUpColumns(int nNumColumns, LPINT nC, LPSTR szFontName, int nFontSize, int nStyle);
  126. int     FAR PASCAL    PrintColumnText(LPSTR szText);
  127. int     FAR PASCAL    EndColumnPrinting(VOID);
  128. int     FAR PASCAL    PrintColumnHeaders(LPSTR szHeader, int nNumColumns, LPINT nC, LPSTR szFontName, int nFontSize, int nStyle);
  129. HDC                 GetPrinterDC(void);
  130. BOOL    FAR PASCAL    KillJob(void);
  131. BOOL    FAR PASCAL    PrinterName(LPSTR szPrinterName);
  132. BOOL    FAR PASCAL    PrinterPort(LPSTR szPortName);
  133. BOOL    FAR PASCAL    IsPageDirty(void);
  134. int     FAR PASCAL    PagePosY(void);
  135. int     FAR PASCAL    PagePosX(void);
  136. int     FAR PASCAL    PageSizeY(void);
  137. int     FAR PASCAL    PageSizeX(void);
  138. BOOL    FAR PASCAL    MoveYPos(int nY);
  139. int     FDrawLine(HDC hDC, int nStartX, int nStartY, int nEndX, int nEndY);
  140. int     FDrawRectangle(HDC hDC, int nStartX, int nStartY, int nEndX, int nEndY);
  141. int     FDrawRndRectangle(HDC hDC, int nStartX, int nStartY, int nEndX, int nEndY, int nElpX, int nElpY);
  142. int     FDrawEllipse(HDC hDC, int nStartX, int nStartY, int nEndX, int nEndY);
  143. BOOL    FAR PASCAL    DrawLine(int nX1, int nY1, int nX2, int nY2);
  144. BOOL    FAR PASCAL    DrawRectangle(int nX1, int nY1, int nX2, int nY2);
  145. BOOL    FAR    PASCAL    DrawRndRectangle(int nX1, int nY1, int nX2, int nY2, int nX3, int nY3);
  146. BOOL    FAR PASCAL    DrawEllipse(int nX1, int nY1, int nX2, int nY2);
  147.  
  148. /*************************************************************************
  149.     DLL initialization, called once when loaded
  150. *************************************************************************/
  151.  
  152. int FAR PASCAL LibMain(HANDLE hInstance, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine)
  153. {
  154.     hInst = hInstance;        // Save this for later, we may need it
  155.  
  156.     /* Set all structure members to zero. */
  157.  
  158.     _fmemset(&ppPrinter, 0, sizeof(PRINTDLG));
  159.  
  160.     return TRUE;
  161.  
  162. }
  163.  
  164.  
  165. /*************************************************************************
  166.     Initialize Printer Driver and set defaults
  167. *************************************************************************/
  168.  
  169. int FAR PASCAL InitializePrinter(HWND hWnd)
  170. {
  171.  
  172.     char szQueueName[80]={""};
  173.  
  174.     hWndApp = hWnd;            // Save the calling apps window handle so we can put up dialogs
  175.  
  176.     fAbort = FALSE;
  177.  
  178.     PageInfo.bPageDirty = FALSE;
  179.  
  180.     ppPrinter.hDC = GetPrinterDC();
  181.  
  182.     if (ppPrinter.hDC)
  183.     {
  184.         // Do stuff for AbortProc and Printing Dialog
  185.  
  186.         lpfnAbortProc = MakeProcInstance(PrnAbortProc, hInst);
  187.         Escape(ppPrinter.hDC, SETABORTPROC, NULL, (LPSTR)(long)lpfnAbortProc, (LPSTR)NULL);
  188.  
  189.            GetUnprintableRegion(ppPrinter.hDC);
  190.            GetPrinterResolution(ppPrinter.hDC);
  191.            LoadString(hInst, IDS_QUEUE_NAME, (LPSTR)szQueueName, 80);
  192.         Escape(ppPrinter.hDC, STARTDOC,lstrlen((LPSTR)szQueueName) ,(LPSTR)szQueueName, NULL);
  193.  
  194.         // Temp Defaults
  195.  
  196.         // Setup defaults
  197.  
  198.         // Set Margins to 1" all around
  199.         PrinterInfo.rcMargins.top =
  200.         PrinterInfo.rcMargins.bottom =
  201.         PrinterInfo.rcMargins.left =
  202.         PrinterInfo.rcMargins.right = (int)(INCH * (PrinterInfo.ptResolution.x * 0.01));
  203.  
  204.         // Start at upper left hand corner
  205.         PageInfo.ptCurrentPos.x = PrinterInfo.rcMargins.left;
  206.         PageInfo.ptCurrentPos.y = PrinterInfo.rcMargins.top;
  207.  
  208.         // Default Tab Stops = 1/2"
  209.         PageInfo.nTabStop = (int)(HALF_INCH * (PrinterInfo.ptResolution.x * 0.01));
  210.  
  211.